{{include file="header.html"}}
	<div class="header loginhead">
		<div class="nav">
		    <div class="title">购物车</div>
		    <div class="right edit">
		        <a href="javascript:" class="edit1">编辑</a>
		    	<a href="javascript:" class="edit2" style="display:none">完成</a>
		    </div>
		    <div id="screenIfmNav" class="screenIfm" onclick="$('#sub-title').toggle();$(this).hide();"></div>
		</div>
	</div>
	<div class="container">
		<div class="cart">
			{{if $result}}
			{{foreach item=val from=$result key=key}}
			<div class="list">
				<div class="items">
					<div class="check">
						<input type="checkbox" value="{{$val.id}}" id="ck{{$key+1}}" name="shop_car[]" class="check_id" goods_id="{{$val.goods_id}}" totalPrice="{{$val.price}}" total="1">
						<label class="cart-checkbox" for="ck{{$key+1}}">
						</label>
					</div>
					<div class="c_item">
						<a class="cell1" href="index.php?app=Goods&act=detail&id={{$val.goods_id}}">
							<img class="cart-photo-thumb" alt="" src="{{$val.image|photo}}">
						</a>
						<div class="cell2">
							<div class="title">
								<a href="index.php?app=Goods&act=detail&id={{$val.goods_id}}">
									<!-- <span>已更新至(第28888云)</span> -->
									{{$val.goodsname}}
								</a>
							</div>
							<div class="cell3">
								<span class="shp-cart-item-price number{{$key}}">
									剩余<i>{{$val.partake}}</i>件
								</span>
								<span class="shp-cart-item-price">
									，<i>￥{{$val.price}}</i>
								</span>
							</div>
							
						</div>
						<div>
							<div class="buycount">
								<div class="buycount-ctrl" attr="number{{$key}}" total="{{$val.partake}}">
									<a href="javascript:void(0);" class="ctrl minus disabled">
										<span class="horizontal">
										</span>
									</a>
									<input value="1" autocomplete="off" type="number" onkeyup='this.value=this.value.replace(/[^0-9]D*$/,"")' ondragenter="return false" onpaste="return !clipboardData.getData('text').match(/D/)" max="5" min="1" class="total"  price="{{$val.price}}">
									<a href="javascript:void(0);" class="ctrl plus">
										<span class="horizontal">
										</span>
										<span class="vertical">
										</span>
									</a>
									
								</div>
								<div class="numtxt">人次</div>
							</div>
							<a onclick="checked({{$val.id}})"><div class="wastebin"></div></a>
						</div>
					</div>
				</div>
				<div class="clear"></div>
			</div>
			{{/foreach}}
			{{/if}}
			<div style="height:100px">
			</div>
			<script type="text/javascript">
				$(function(){
					
					
				})
			</script>
			<div class="payment-total-bar payment-total-bar-new" id="payment">
				<!-- <div class="autonew shp-chk-new">
					<i class='inline' href="#inline_content"></i>
					<input type="checkbox" value="1" id="cka" checked="checked">
					<label class="cart-checkbox" for="cka">
					</label>
					<label class="cart-checkbox-text" for="cka">
						如被抢光自动参与最新一云
					</label>
					
					<div style='display:none'>
						<div id='inline_content' style='background:#fff;'>
							<div class="comfirm">
								<h3>温馨提示</h3>
								<p>热门商品可能会瞬间抢光哦，此处勾选后系统之自动为您更新最新一云!</p>
								<span class="sure">确定</span>
							</div>
						</div>
					</div>
					
					<script type="text/javascript">
						$(document).ready(function(){
							var window_width  = $(window).width();
							if(window_width<720){
								$(".inline").colorbox({inline:true, width:"90%",overlayClose:false,fixed:true});
							}else{
								$(".inline").colorbox({inline:true, width:"700px",overlayClose:false,fixed:true});
							}
							$('.sure').click(function(){
								parent.$.fn.colorbox.close(); //关闭窗口
							})
						});
					</script>
				</div> -->
				
				<div class="shpbox">
					<div class="shp-chk-new all">
						<input type="checkbox" value="1" id="cka1">
						<label class="cart-checkbox" for="cka1">
						</label>
						<label class="cart-checkbox-text" for="cka1">
							全选
						</label>
					</div>
					<div class="shp-cart-info">
						<strong class="shp-cart-total">
							合计:
							<span class="bottom-bar-price">
								￥0.00
							</span>
						</strong>
					</div>
					<a class="btn-gave btn-right-block-new" id="gotopay">
						结算(
						<span id="checkedNum">
							
						</span>
						)
					</a>
					<a class="btn-delete btn-right-block-new" style="display:none;">
						删除
					</a>
					<div class="clear"></div>
				</div>
			</div>
		</div>
		{{if !$result}}
		<div class="empty">
			<i class="icon-sc"></i>
			<p class="title"><span>╯▂╰ 购物车都饿扁了，快去喂它吧</span></p>
			<br>
			<a class="btn btn-large" href="index.php?app=Goods">去购物</a>
		</div>
		{{/if}}
	</div>
	<script type="text/javascript">
		$(function(){
			$('.edit').click(function(){
				$('.items .check,.btn-delete,.btn-gave,.edit1,.edit2,.autonew,.shp-cart-total,.all,.list .title,.wastebin').toggle();
				$('.items').toggleClass('checkin');
			})
			//全选
			$('.all input').change(function(){
				if($(this).is(':checked')){
					$('.list .check input').attr('checked',true);
				}else{
					$('.list .check input').attr('checked',false);
				}
			})
			//结算数目
			$('#checkedNum').text($('.list').length);
			
			//编辑完成后结算数目
			$('.edit2').click(function(){
				$('#checkedNum').text($('.list').length);
				amount();
				empty();
			})
			function empty(){
				if($('.list').length==0){
					$('.empty').show();
					$('.cart').hide();
				}else{
					$('.empty').hide();
					$('.cart').show();
				}
			}
			empty();
			//合计
        	amount();
		})

		//增加人次
		 $(".plus").click(function(){
        	var total = $(this).parent().find("input").val();
        	var total = (total*1)+1;//参与人数
        	if(total>5) return false;
        	var price = $(this).prev().attr('price');//单价
	        var totalprice = (price*1)*(total*1);
	        $(this).parents(".items").find(".check_id").attr('totalPrice',totalprice);//数目*单价
	        $(this).parents(".items").find(".check_id").attr('total',total);//数目
        	amount();
        	number(total,$(this));
        })

		 //减少人次
        $(".minus").click(function(){
        	var total = $(this).parent().find("input").val();
        	var total = (total*1)-1;
        	if(total<=0) return false;
        	var price = $(this).next().attr('price');
	        var totalprice = (price*1)*(total*1);
	        $(this).parents(".items").find(".check_id").attr('totalPrice',totalprice);//数目*单价
        	$(this).parents(".items").find(".check_id").attr('total',total);//数目
        	amount();
        	number(total,$(this));
        })

        //
        $(".buycount-ctrl input").change(function(){
        	var total = $(this).val();
        	var price = $(this).attr('price');
	        var totalprice = (price*1)*(total*1);
	        $(this).attr('totalPrice',totalprice);
        	amount();
        	number(total,$(this));
        })

        function number(number,val){
        	var identify = val.parent().attr('attr');//标识
        	var total = val.parent().attr('total');//最初剩余人数
        	var surplus = total*1-number*1;
        	$("."+identify+" i").text(surplus);//计算所得剩余人数
        }

        //购物车ids
        function getAllIds(){
        	var ids = "";
        	$('.check_id:checked').each(function(i){
				ids += (","+$(this).val());
		    });
		    return ids;
        }

         //购物车ids
        function getgoodsid(){
        	var ids = "";
        	$('.check_id:checked').each(function(i){
				ids += (","+$(this).attr('goods_id'));
		    });
		    return ids;
        }

        function deletecar(ids){
        	ui.confirm('╯▂╰,真的不要我了？',function(z){
				if(z){
					$.get('{{"index.php?app=Car&act=delete"|URL}}',{'ids':ids},function(result){
				        if(result.statusCode=='200'){
				        	ui.success('清空成功！');
				        	$.cookie('car_number',result.message,{expires: null,path: "/"});
				        	$("#shopCart i").text(result.message);
				        	setTimeout(function(){
				        		window.location.reload();
							},1000);
				        } 
				        else ui.error(result.message);
				    },'json');
				}
	        },true);
        }

        //单挑购物车进行删除
        function checked(id){
        	deletecar(id);
        }

        //多条购物车进行删除
        $(".btn-delete").click(function(){
        	if($('.check_id:checked').length==0) { ui.alert("至少选择一条数据!"); return false; }
			var ids = getAllIds();
			deletecar(ids);
        })



        //购买的总人次，组成字符串
        function total(){
        	var number = "";
        	$('.check_id:checked').each(function(i){
				number += (","+$(this).attr('total'));
		    });
		    return number;
        }
        //合计
        function amount(){
        	var price = '';
        	$(".check_id:checked").each(function(){
	            price = price*1+($(this).attr('totalPrice')*1);
	        })
        	var ex = /^\d+$/;
			if(ex.test(price)) $('.bottom-bar-price').text("￥"+(price)+'.00');
			else $('.bottom-bar-price').text("￥"+(price));
			if(price == '')
				$('.bottom-bar-price').text("￥0.00");
        	return true;
        }

        $('#gotopay').on('click',function(e){
			 // if('{{$member.id}}'==''){
				//  $.cookie('history_url', window.location.href, {expires: null,path: "/"});
				//  document.location.href='{{"index.php?app=member&act=login"|URL}}';
			 // }else{
			var auto  = $('#cka').attr('checked') ? 1 : 0;
			var ids=getAllIds();//购物id
			var totals = total();
			var goodsid = getgoodsid();
			if(ids==''){
				ui.alert('请选择要结算的商品!');
			}else{
				$.post('{{"index.php?app=pay&act=check"|URL}}',{'ids':ids.substr(1)},function(result){
			        if(result.statusCode=='200'){
			        	location.href ='{{"index.php?app=pay"|URL}}?ids='+ids.substr(1)+'&total='+totals.substr(1)+'&goodsid='+goodsid.substr(1);
			        }else if(result.statusCode == '201'){
			        	ui.alert(result.message);
			        }else ui.error(result.message);
			    },'json');
				
			}
			 // }
		});
	</script>
	<!-- 底部 -->
{{include file="menu2.html"}}